package com.tgy.dao;

import com.tgy.entity.User;
import com.tgy.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/***
 * @ClassName: UserMapperTset
 * @Description:
 * @Auther: 送一一块二向箔
 * @Date: 15:16 2021/12/14
 * @version : V1.0
 */
public class UserMapperTset {
    @Test
    public void getList() {
        //得到sqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        /*方法一。getmapper*/
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getUsers();
        System.out.println(users.toString());
        sqlSession.close();
    }

    @Test
    public void getById() {
        //得到sqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        /*方法一。getmapper*/
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectUserById(1);
        System.out.println(user.toString());
        sqlSession.close();
    }
    @Test
    public void insert() {
        //得到sqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        /*方法一。getmapper*/
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = new User(4, "eqqwe", "ewe");
        int i = mapper.insertUser(user1);
        //因为没有开启自动提交事务，所以需要手动提交事务
        sqlSession.commit();
        sqlSession.close();
        System.out.println(i);
    }
    @Test
    public void update() {
        //得到sqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        /*方法一。getmapper*/
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = new User(4, "小唐", "123456");
        int i = mapper.updUser(user1);
        sqlSession.commit();
        sqlSession.close();
        System.out.println(i);
    }
    @Test
    public void delete() {
        //得到sqlSession对象
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        /*方法一。getmapper*/
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.delUser(4);
        //提交事务
        sqlSession.commit();
        sqlSession.close();
        System.out.println(i);
    }
    @Test
    public void like(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getUsersLike("李");
        System.out.println(users);


        sqlSession.close();
    }
}
